'use strict';

/* Controllers */
app.controller('houseOwnerRepayLoanEndListCtl', function($scope,$compile,ngDialog,uiLoad,$state,rest,user) {
    var rentTypeObj = {
        0: "年付",
        1: "季付",
        2: "月付"
    };
    var applyTypeObj = {
        0: "支付包",
        1: "微信",
        2: "现金"
    };
    $scope.checkType = {};
    $scope.houseType = {}
    $scope.checkTypeArray = [{ "name": "未申请", "value": "0" }, { "name": "申请中", "value": "1" }, { "name": "申请通过", "value": "2" }, { "name": "装修中", "value": "3" }, { "name": "装修完成", "value": "3" }];
    if($scope.queryParams){

    }
    $scope.queryParams = {
        'decorateStatus': $scope.houseType.selected || 0,
        'startTime': $scope.startTime,
        'endTIme': $scope.endTIme,
        'roomCode': $scope._roomCode
    }

    rest.post({
        methodId: 'J018',         
         data: {'accountOpenId': user.get('openId')},
       // data: {'accountOpenId': '1dca25eabc753b7087ef659a5d7c5c98'},
        success: function(res) {
           // console.log(res);
           var numAmount = res.data.realAmount.substring(0,res.data.realAmount.length -2);    
           $scope.totalAmount = numAmount;
           $scope.totalAmountLeft = numAmount;
           $scope.$apply();
        },
        error: function() {
           
        }
    })
    
    $scope.tableLoanDatas = {
        "sAjaxSource": "4033",
        "aoColumns": [
        // {
        //     "mDataProp": "areaName",
        //     "sTitle": "小区名称",
        //     "sDefaultContent": "",
        //     "sClass": "center"
        // }, {
        //     "mDataProp": "address",
        //     "sTitle": "详细地址",
        //     "sDefaultContent": "",
        //     "sClass": "center"
        // }, {
        //     "mDataProp": "roomCode",
        //     "sTitle": "房间号",
        //     "sDefaultContent": "",
        //     "sClass": "center"
        // },
          {
            "mDataProp": "totalAmount",
            "sTitle": "放款金额",
            "sDefaultContent": "",
            "sClass": "center"
        }, 
        // {
        //     "mDataProp": "loanDate",
        //     "sTitle": "放款时间",
        //     "sDefaultContent": "",
        //     "sClass": "center loanDate"
        // }, 
        {
            "mDataProp": "repaymentDateString",
            "sTitle": "还款周期",
            "sDefaultContent": "",
            "sClass": "center"
        },

        // {
        //     "mDataProp": "installment",
        //     "sTitle": "已还/期数",
        //     "sDefaultContent": "",
        //     "sClass": "center installAndHavePay"
        // },
        {
            "mDataProp": "status",
            "sTitle": "还款状态",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": "operate",
            "sTitle": "操作",
            "sDefaultContent": "",
            "sClass": "center handle-td"
        }],
        "fnRowCallback": function(nRow, aData, iDisplayIndex ) {

            var btnStr = "";
            btnStr += "<a href='javascript:;' class='btn btn-yellow' ng-click='godetail(\"" + aData.orderId + "\")' style='margin-right:10px;'>查看</a>";
            function time(mydate){
              var str = mydate.substring(0,10);
              var dataArr = str.split('-');
              var timeStr = dataArr[0]
                +"年"+dataArr[1]+"月"+dataArr[2]
                +"日";
                return timeStr;
            }

            var installAndHavePay = aData.havePay + "/" + aData.installment;
            $(nRow).find(".installAndHavePay").empty().append(installAndHavePay);
            $(nRow).find(".loanDate").empty().append(time(aData.loanDate));
            $(nRow).find(".handle-td").append($compile(btnStr)($scope));
        },
        // "fnDrawCallback": function(oSettings) {
        //     $scope.houseTypeValue = $scope.queryParams.houseType;
        //     if ("number" == typeof $scope.houseTypeValue) {
        //         $scope.checkTypeArray.map(function(ele, index) {
        //             if ($scope.houseTypeValue == ele.value) {
        //                 $scope.houseType.selected = ele;
        //             }
        //         })
        //     }
        // },
        "fnServerParams": function(aoData) {
            // if($scope.houseType && $scope.houseType.selected){
            //     $scope.queryParams.houseType = $scope.houseType.selected.value
            // }

            aoData.push({
                'name': 'queryParams',
                'value': $scope.queryParams
            });
            return aoData;
        }
    };

    // $scope.$watch('_roomCode', function(newValue, oldValue) {
    //      $scope.queryParams.roomCode = newValue;
    // })
    
    $scope.queryUser = function() {
        if($scope.queryParams.houseType && $scope.queryParams.houseType.selected){           
            if($scope.queryParams.houseType.selected){
                $scope.houseType = $scope.queryParams.houseType.selected.value
            }
            $scope.queryParams = {
                'roomCode': $scope._roomCode
            }
            $scope.dataTable.fnClearTable();
            $scope.queryParams = {};           
        }
        else{
            $scope.queryParams = {
                'roomCode': $scope._roomCode
            }
            console.log($scope.queryParams);
            $scope.dataTable.fnClearTable();   
            $scope.queryParams = {};  
        } 
    };

    $scope.godetail = function (orderId) {
      ngDialog.openConfirm({
          template: 'views/houseOwner/loanRepayEndDetail.html',
          resolve: {
            load: function() {
              return uiLoad.load(['js/controllers/houseOwner/repayLoanEndDetailCtr.js'])
            }
          },
          data:{
            orderId:orderId
          },
          scope: $scope,
          width: 1200,
          // height: 700
        }).then(function(data) {

        }, function(reason) {

        });

    }

       $scope.charge = function() {
          ngDialog.openConfirm({
          template: 'js/module/fund/charge.html',
          resolve: {
            load: function() {
                return uiLoad.load(['js/module/fund/charge.js'])
            }
          },
          data:{
            
          },
          scope: $scope,
          width: 600,
          // height: 700
        }).then(function(data) {

        }, function(reason) {

        });
     }
     

    $scope.dataTable = undefined;

          // "landlordId": "40155814-52b1-4cf2-814a-724f770bbd5b",
          // "decorateId": "5bea9b36-1f94-4bf6-af8a-e2ed3117f53b",
          // "houseId": "e55c6e9f-d6b4-46d0-94c5-e20063ee0611",

    $scope.decorateWithdraw = function (decorateId) {

    }

    $scope.decorateReview = function (decorateId) {
        ngDialog.openConfirm({
          template: 'js/module/decorate/review.html',
          resolve: {
            load: function() {
              return uiLoad.load(['js/module/decorate/review.js'])
            }
          },
          data:{
            decorateId:decorateId
          },
          scope: $scope,
          width: 1200,
          // height: 700
        }).then(function(data) {

        }, function(reason) {

        });
    }

    
    // 回调函数,返回实例化的jquery 插件对象
    $scope.initFinish = function(dataTable) {
        $scope.dataTable = dataTable;
    }
    $('#example thead').on('click', 'th', function() {
        // var data = $('#example').DataTable().row( this ).data();
        // alert('You clicked on  row');
    });
    $('#example tbody').on('click', 'span#edit', function() {
        var data = $('#example').DataTable().row($(this).parents('tr'));
        alert("查看修改：" + data[0]);
    });
    $('#example tbody').on('click', 'span#del', function() {
        // var data = $('#example').DataTable().row($(this).parents('tr')).data();
    });
});
